package com.maila.xiaoaoschool.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.cjj.MaterialRefreshLayout;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.adapter.ScoreChangeAdapter;
import com.maila.xiaoaoschool.api.FindApi;
import com.maila.xiaoaoschool.entity.ScoreGoods;
import com.maila.xiaoaoschool.net.VolleyInterface;
import com.maila.xiaoaoschool.widget.NoScrollGridView;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/9/29.
 */

public class ScoreActivity extends AppCompatActivity {
    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.tv_shop_namee)
    TextView tvShopNamee;
    @BindView(R.id.ll_title)
    AutoLinearLayout llTitle;
    @BindView(R.id.rb_core)
    AutoLinearLayout rbCore;
    @BindView(R.id.rb_recode)
    AutoLinearLayout rbRecode;
    @BindView(R.id.lv_fujin)
    NoScrollGridView lvFujin;
    @BindView(R.id.hot_refresh)
    MaterialRefreshLayout hotRefresh;
    ScoreChangeAdapter scoreChangeAdapter;
    @BindView(R.id.tv_score)
    TextView tvScore;
    private int page = 1;
    private int size = 50;
    Gson gson = new Gson();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);
        ButterKnife.bind(this);
        scoreChangeAdapter = new ScoreChangeAdapter(this);
        lvFujin.setAdapter(scoreChangeAdapter);
        initdata();
    }

    /**
     * 初始化数据
     */
    private void initdata() {
        //获取个人积分
        new FindApi(this, new VolleyInterface() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.optInt("result") == 1) {
                        String num = jsonObject.optString("data");
                        tvScore.setText(num);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        }).getSelfScore();

        getscorgoodslist();
    }

    /**
     * 获取积分商品列表
     */
    private void getscorgoodslist() {
        new FindApi(this, new VolleyInterface() {
            @Override
            public void onSuccess(String result) {
                try {
                    ScoreGoods scoreGoods = gson.fromJson(result,ScoreGoods.class);
                    if(scoreGoods.getResult()==1){
                        if(page==1){
                            scoreChangeAdapter.setMdata(scoreGoods.getData());
                        }else {
                            scoreChangeAdapter.addMdata(scoreGoods.getData());
                        }
                      ++page;
                    }else {

                    }

                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        }).getscoregoods(page, size);
    }

    private void requestViewFoucus(View view) {
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestViewFoucus(tvShopNamee);
    }

    @OnClick({R.id.white_return_iv, R.id.rb_recode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.white_return_iv:
                finish();
                break;
            case R.id.rb_recode:
                break;
        }
    }
}
